package com.cyw.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.cyw.common.utils.StringUtils;
import com.cyw.common.core.page.TableDataInfo;
import com.cyw.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cyw.system.mapper.SysUserMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.cyw.system.domain.bo.TbTopicBo;
import com.cyw.system.domain.vo.TbTopicVo;
import com.cyw.system.domain.TbTopic;
import com.cyw.system.mapper.TbTopicMapper;
import com.cyw.system.service.ITbTopicService;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 话题Service业务层处理
 *
 * @author myproject
 * @date 2022-11-23
 */
@RequiredArgsConstructor
@Service
public class TbTopicServiceImpl implements ITbTopicService {

    private final TbTopicMapper baseMapper;

    @Resource
    private SysUserMapper sysUserMapper;

    /**
     * 查询话题
     */
    @Override
    public TbTopicVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询话题列表
     */
    @Override
    public TableDataInfo<TbTopicVo> queryPageList(TbTopicBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TbTopic> lqw = buildQueryWrapper(bo);
        Page<TbTopicVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询话题列表
     */
    @Override
    public List<TbTopicVo> queryList(TbTopicBo bo) {
        LambdaQueryWrapper<TbTopic> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TbTopic> buildQueryWrapper(TbTopicBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TbTopic> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getTopicName()), TbTopic::getTopicName, bo.getTopicName());
        lqw.eq(StringUtils.isNotBlank(bo.getImageUrl()), TbTopic::getImageUrl, bo.getImageUrl());
        lqw.eq(bo.getUserCount() != null, TbTopic::getUserCount, bo.getUserCount());
        lqw.eq(StringUtils.isNotBlank(bo.getTopicDesc()), TbTopic::getTopicDesc, bo.getTopicDesc());
        lqw.eq(StringUtils.isNotBlank(bo.getTopicType()), TbTopic::getTopicType, bo.getTopicType());
        return lqw;
    }

    /**
     * 新增话题
     */
    @Override
    public Boolean insertByBo(TbTopicBo bo) {
        TbTopic add = BeanUtil.toBean(bo, TbTopic.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改话题
     */
    @Override
    public Boolean updateByBo(TbTopicBo bo) {
        TbTopic update = BeanUtil.toBean(bo, TbTopic.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TbTopic entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除话题
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List getAll() {
        LambdaQueryWrapper<TbTopic> wrapper = Wrappers.lambdaQuery();
        wrapper.orderByDesc(TbTopic::getUserCount);
        return baseMapper.selectList(wrapper);
    }
}
